package com.study.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.mybatisplus.entity.User;
import com.study.mybatisplus.mapper.UserMapper;
import com.study.mybatisplus.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.io.Serializable;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author flwu
 * @since 2020-03-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public Integer add(User user) throws Exception {
        Integer result = userMapper.insert(user);
        System.out.println(result);
        System.out.println(user.getId());
        return user.getId().intValue();
    }

    @Transactional
    @Override
    public User info(Integer id) throws Exception {
        User user  = userMapper.selectById(id);
        return user;
    }

    @Transactional
    @Override
    public Boolean udpate(User user, Integer id) throws Exception {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        Boolean result = false;
        try {
            Integer rs = userMapper.update(user, queryWrapper);
            if (rs > 0) {
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public User findUserByPhone(String phone) throws Exception {
        User userInfo = userMapper.findUserMapByPhone(phone);
        return userInfo;
    }

    @Override
    public Map<String, Object> findUserById(Integer id) throws Exception {
        return userMapper.findUserMapById(id);
    }
}
